var express = require("express");
var router = express.Router();
var crud = require("./crud.js");
var url = require("url");
var Mock = require('mockjs');
//动态
var commentData=require("./dynamicData/commentData.js")
var letterData = require("./dynamicData/privateLetter.js")
var systemData = require("./dynamicData/systemData.js")
//mine
var mineIndexData = require('./mineData/mineIndex.js')

//片花
var filmInfo=require('./filmData/film/filmInfo.js');
var filmCommentInfo=require('./filmData/comment/comment.js');
var filmReviewInfo=require('./filmData/reviewData/reviewInfo.js');
var videoInfo=require('./filmData/film/videoInfo.js');
var peopleInfo=require('./filmData/people/peopleInfo.js');

//圈子
var commentInfo=require('./circleData/commentdata.js');

router.all("*", function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
});

// 注册接口
router.post("/adduser", function(req, res) {
	// 获取参数
	var username = req.body.username;
	var password = req.body.password;

	var sql = "insert into user (username,password) values (?,?)";
	var arr = [username, password];
	console.log(arr)
	crud.insertData(sql, arr, function(result) {
		
		if (result) {
			res.json(200, {
				"msg": "success"
			})
		} else {
			res.json(200, {
				"msg": "error"
			})
		}
	})
});


// 登录接口
router.post("/selectuser", function(req, res) {
	// 获取参数
	var username = req.body.username;
	var password = req.body.password;
	var sql = "select * from user where username=?";
	var arr = [username];
	crud.selectData(sql, arr, function(result) {
		if (result.length > 0) {
			res.json(200, result); // {username:"",password:""}
		} else {
			res.json(200, {
				"msg": "暂无注册"
			})
		}
	})
});
// 修改密码接口
router.post("/resetpassword", function(req, res) {
	// 获取参数
	var username = req.body.username;
	var password = req.body.password;
	var sql = "update user set `password`=? where username=?";
	var arr = [password, username];
	crud.updateData(sql, arr, function(result) {
		if (result) {
			res.json(200, {
				"msg": "success"
			})
		} else {
			res.json(200, {
				"msg": "error"
			})
		}
	})
})

//模拟视频接口
router.get('/filmList',function(request,response){
	var data = Mock.mock({
	    'filmList|1-10': filmInfo
    })
	response.json(200,data)
	
})

//模拟视频评论接口
router.get('/filmCommentList',function(request,response){
	var data = Mock.mock({
	    'filmCommentList|1-5': filmCommentInfo
    })
	response.json(200,data)
	
})


//模拟视频回复接口
router.get('/filmReviewList',function(request,response){
	var data = Mock.mock({
	    'filmReviewList|1-7': filmReviewInfo
    })
	response.json(200,data)
	
})

//模拟视频信息接口
router.get('/videoInfo',function(request,response){
	var data = Mock.mock({
	    'videoInfo|1-10': videoInfo
    })
	response.json(200,data)
	
})

//模拟人物信息接口
router.get('/peopleInfo',function(request,response){
	var data = Mock.mock({
	    'peopleInfo|1-10': peopleInfo
    })
	response.json(200,data)
	
})

//模拟我的评论接口
router.get("/commentData",function(request,response){
	var comData = Mock.mock({
		"commentData|1-3":[
		   { "comment|+1":commentData}
		]
	});
	response.json(200,comData);
})
//模拟我的播放历史filmInfo
router.get('/filmHistory',function(request,response){
	var data = Mock.mock({
	    'filmList|1-6': [
	      {
	      	"filmData|+1":filmInfo
	      }
	    ]
    })
	response.json(200,data)
	
})

//模拟我的关注接口
router.get("/guanZhuData",function(request,response){
	var gzData = Mock.mock({
		"guanZhuData|6":[
		   { "guanZhu|+1":commentData}
		]
	});
	response.json(200,gzData);
})

//模拟私信的接口
router.get("/letterData",function(request,response){
	var leData = Mock.mock({
		"data|1-4":[{
			"leData|+1":letterData
		}]
	});
	response.json(200,leData);
})
//模拟系统消息的接口
router.get("/systemData",function(request,response){
	var systemList = Mock.mock({
		"systemInfo|1-3":[{
			"systemData|+1":systemData
		}]
	});
	response.json(200,systemList);
})
//模拟mineIndex的接口
router.get("/mineIndexData",function(request,response){
	var mineList=Mock.mock({
		"data|1":mineIndexData
	});
	response.json(200,mineList);
})

//圈子评论内容接口
router.get('/commentList',function(require,response){
	var data = Mock.mock({
		'commentList|1-5':commentInfo
	})
	response.json(200,data.commentList)
})

module.exports = router;